document.addEventListener('DOMContentLoaded', (event) => {
    // 获取文章列表并显示在页面上
    fetch(`http://localhost:8888/articles/list`)
    .then(response => response.json())
    .then(data => {
        console.log(data);  // 调试用
        const articleList = document.querySelector('.article-list');
        articleList.innerHTML = ''; // 清空当前文章列表

        if (data.code === 200) {
            data.data.forEach(article => {
                const articleElement = document.createElement('div');
                articleElement.className = 'article';
                articleElement.innerHTML = `
                    <h2>${article.title}</h2>
                    <p>${article.summary}</p>
                    <div class="article-footer">
                        <span>作者: ${article.account}</span>
                        <span>发布时间: ${new Date(article.createDate).toLocaleDateString()}</span>
                        <button onclick="redirectToDetail(${article.id})">查看全文>></button>
                    </div>
                `;
                articleList.appendChild(articleElement);
            });
        } else {
            articleList.innerHTML = '<p>无法获取文章列表</p>';
        }
    })
    .catch(error => {
        console.error('Error fetching articles:', error);
        document.querySelector('.article-list').innerHTML = '<p>无法获取文章列表</p>';
    });
});

function redirectToDetail(articleId) {
    window.location.href = `blog_detail.html?articleId=${articleId}`;
}
